﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using eDoc.DAL;
using System.Data;
namespace eDoc.BO
{
    public class Data
    {
        public static void InsertAppPlist(string AppKey, string AppName, string AppVersion, string AppBundleIdentifier, string AppDeviceFamily, string AppMinimumOSVersion, int AppSize, string AppPlistPath, string AppUrlInstall, string AppCFBundleSignature, string AppIcon)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Insert_AppPlist(AppKey, AppName, AppVersion, AppBundleIdentifier, AppDeviceFamily, AppMinimumOSVersion, AppSize, AppPlistPath, AppUrlInstall, AppCFBundleSignature, AppIcon);
                db.Close();
            }
        }

        public static void InsertAppPlist(string AppKey, string AppName, string AppVersion, string AppBundleIdentifier, string AppDeviceFamily, string AppMinimumOSVersion, int AppSize, string AppPlistPath, string AppUrlInstall, string AppCFBundleSignature, string AppIcon, DateTime buildTime)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Insert_AppPlist(AppKey, AppName, AppVersion, AppBundleIdentifier, AppDeviceFamily, AppMinimumOSVersion, AppSize, AppPlistPath, AppUrlInstall, AppCFBundleSignature, AppIcon, buildTime);
                db.Close();
            }
        }


        /// <summary>
        /// Insert thong tin cua ung dung
        /// </summary>
        /// <param name="AppKey">Key tu sinh cua ung dung</param>
        /// <param name="AppName">Ten ung dung</param>
        /// <param name="AppDisplayName">Ten hien thi ung dung</param>
        /// <param name="AppVersion">Phien ban cua ung dung</param>
        /// <param name="AppBundleIdentifier">Bundle Identifier</param>
        /// <param name="AppDeviceFamily">Device Family</param>
        /// <param name="AppMinimumOSVersion">Minimum OS Version</param>
        /// <param name="AppSize">Size</param>
        /// <param name="AppPlistPath">Duong dan file plist</param>
        /// <param name="AppUrlInstall">Duong dan file ipa de install</param>
        /// <param name="AppCFBundleSignature">Chu ky cua ung dung</param>
        /// <param name="AppIcon">Icon cua ung dung</param>
        /// <param name="BuildTime">Thoi gian build ung dung</param>
        /// <param name="AppMode">Loai ung dung duoc su dung theo nhan vien</param>
        /// <param name="AppDesc">Mo ta ung dung</param>
        /// <param name="isPublished">Trang thai : Published hay Testing</param>
        /// <param name="AppJB">Ung dung Jailbreak</param>
        /// <param name="AppPlatformVersion">Xac dinh version hien tai cua ung dung co cai dc tren may hay ko</param>
        /// <param name="AppPlatformName">Platform Name</param>
        public static void InsertAppPlist(string AppKey, string AppName, string AppDisplayName, string AppVersion, string AppBundleIdentifier, 
                        string AppDeviceFamily, string AppMinimumOSVersion, int AppSize, string AppPlistPath, string AppUrlInstall, 
                        string AppCFBundleSignature, string AppIcon, DateTime BuildTime, int AppMode, string AppDesc, bool isPublished, 
                        bool AppJB, string AppPlatformVersion, string AppPlatformName)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Insert_AppPlist(AppKey, AppName, AppDisplayName, AppVersion, AppBundleIdentifier, AppDeviceFamily, AppMinimumOSVersion, AppSize, AppPlistPath, AppUrlInstall, AppCFBundleSignature, AppIcon, BuildTime, AppMode, AppDesc,isPublished, AppJB, AppPlatformVersion, AppPlatformName);
                db.Close();
            }
        }

        /// <summary>
        /// Insert thong tin cua ung dung
        /// </summary>
        /// <param name="AppKey">Key tu sinh cua ung dung</param>
        /// <param name="AppName">Ten ung dung</param>
        /// <param name="AppDisplayName">Ten hien thi ung dung</param>
        /// <param name="AppVersion">Phien ban cua ung dung</param>
        /// <param name="AppBundleIdentifier">Bundle Identifier</param>
        /// <param name="AppDeviceFamily">Device Family</param>
        /// <param name="AppMinimumOSVersion">Minimum OS Version</param>
        /// <param name="AppSize">Size</param>
        /// <param name="AppPlistPath">Duong dan file plist</param>
        /// <param name="AppUrlInstall">Duong dan file ipa de install</param>
        /// <param name="AppCFBundleSignature">Chu ky cua ung dung</param>
        /// <param name="AppIcon">Icon cua ung dung</param>
        /// <param name="BuildTime">Thoi gian build ung dung</param>
        /// <param name="AppMode">Loai ung dung duoc su dung theo nhan vien</param>
        /// <param name="AppDesc">Mo ta ung dung</param>
        /// <param name="isPublished">Trang thai : Published hay Testing</param>
        /// <param name="AppJB">Ung dung Jailbreak</param>
        /// <param name="AppPlatformVersion">Xac dinh version hien tai cua ung dung co cai dc tren may hay ko</param>
        /// <param name="AppPlatformName">Platform Name</param>
        /// <param name="expirationDate">Thoi gian het han ung dung</param>
        public static void InsertAppPlist(string AppKey, string AppName, string AppDisplayName, string AppVersion, string AppBundleIdentifier,
                        string AppDeviceFamily, string AppMinimumOSVersion, int AppSize, string AppPlistPath, string AppUrlInstall,
                        string AppCFBundleSignature, string AppIcon, DateTime BuildTime, int AppMode, string AppDesc, bool isPublished,
                        bool AppJB, string AppPlatformVersion, string AppPlatformName, DateTime expirationDate, string AppPlistContent)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Insert_AppPlist(AppKey, AppName, AppDisplayName, AppVersion, AppBundleIdentifier, AppDeviceFamily, 
                                                            AppMinimumOSVersion, AppSize, AppPlistPath, AppUrlInstall, AppCFBundleSignature,
                                                            AppIcon, BuildTime, AppMode, AppDesc, isPublished, AppJB, AppPlatformVersion, AppPlatformName, expirationDate, AppPlistContent);
                db.Close();
            }
        }

        /// <summary>
        /// Update thong tin cua ung dung
        /// </summary>
        /// <param name="ID">ID cua ung dung</param>
        /// <param name="AppKey">Key tu sinh cua ung dung</param>
        /// <param name="AppName">Ten ung dung</param>
        /// <param name="AppVersion">Phien ban cua ung dung</param>
        /// <param name="AppBundleIdentifier">Bundle Identifier</param>
        /// <param name="AppDeviceFamily">Device Family</param>
        /// <param name="AppMinimumOSVersion">Minimum OS Version</param>
        /// <param name="AppSize">Size</param>
        /// <param name="AppPlistPath">Duong dan file plist</param>
        /// <param name="AppUrlInstall">Duong dan file ipa de install</param>
        /// <param name="AppCFBundleSignature">Chu ky cua ung dung</param>
        /// <param name="AppIcon">Icon cua ung dung</param>
        /// <param name="Published">Published hay Testing</param>
        public static void Update_AppPlist(int ID, string AppKey, string AppName, string AppVersion, string AppBundleIdentifier, string AppDeviceFamily, string AppMinimumOSVersion, int AppSize, string AppPlistPath, string AppUrlInstall, string AppCFBundleSignature, string AppIcon, bool Published)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Update_AppPlist(ID,AppKey, AppName, AppVersion, AppBundleIdentifier, AppDeviceFamily, AppMinimumOSVersion, AppSize, AppPlistPath, AppUrlInstall, AppCFBundleSignature, AppIcon, Published);
                db.Close();
            }
        }

        /// <summary>
        /// Update thong tin cua ung dung
        /// </summary>
        /// <param name="ID">ID cua ung dung</param>
        /// <param name="AppKey">Key tu sinh cua ung dung</param>
        /// <param name="AppName">Ten ung dung</param>
        /// <param name="AppDisplayName">Ten hien thi ung dung</param>
        /// <param name="AppVersion">Phien ban cua ung dung</param>
        /// <param name="AppBundleIdentifier">Bundle Identifier</param>
        /// <param name="AppDeviceFamily">Device Family</param>
        /// <param name="AppMinimumOSVersion">Minimum OS Version</param>
        /// <param name="AppSize">Size</param>
        /// <param name="AppPlistPath">Duong dan file plist</param>
        /// <param name="AppUrlInstall">Duong dan file ipa de install</param>
        /// <param name="AppCFBundleSignature">Chu ky cua ung dung</param>
        /// <param name="AppIcon">Icon cua ung dung</param>
        /// <param name="Published">Published hay Testing</param>
        /// <param name="AppMode">Loai ung dung tuy thuoc theo nhan vien</param>
        /// <param name="AppDesc">Mo ta ung dung</param>
        /// <param name="AppJB">Ung dung Jailbreak</param>
        public static void Update_AppPlist(int ID, string AppKey, string AppName, string AppDisplayName, string AppVersion, string AppBundleIdentifier, string AppDeviceFamily, string AppMinimumOSVersion, int AppSize, string AppPlistPath, string AppUrlInstall, string AppCFBundleSignature, string AppIcon, bool Published, int AppMode, string AppDesc, bool AppJB)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Update_AppPlist(ID, AppKey, AppName, AppDisplayName, AppVersion, AppBundleIdentifier, AppDeviceFamily, AppMinimumOSVersion, AppSize, AppPlistPath, AppUrlInstall, AppCFBundleSignature, AppIcon, Published, AppMode, AppDesc, AppJB);
                db.Close();
            }
        }

         /// <summary>
        /// Delete AppPlist by ID
        /// </summary>
        /// <param name="ID"></param>
        public static void Delete_AppPlist_by_ID(int ID)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Delete_AppPlist_by_ID(ID);
                db.Close();
            }
        }

        /// <summary>
        /// Lay danh sach cac ung dung
        /// </summary>
        /// <returns></returns>
        public static DataTable Select_AppPlist()
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_AppPlist();
                db.Close();
            }

            return dt;
        }

        /// <summary>
        /// Lay danh sach cac ung dung
        /// </summary>
        /// <param name="Status">Trang thai : Published hoac Testing</param>
        /// <returns></returns>
        public static DataTable Select_AppPlist(bool Status)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_AppPlist(Status);
                db.Close();
            }

            return dt;
        }

        /// <summary>
        /// Dang nhap de tai ung dung. Neu gia tri tra ve = 0: sai ten; 1: sai mat khau; 2: thanh cong
        /// </summary>
        /// <param name="Username">Ten dang nhap</param>
        /// <param name="Password">Mat khau</param>
        /// <returns></returns>
        public static int LoginDownload(string Username, string Password)
        {
            int value = -2;

            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_LoginDownload(Username, Password);
                db.Close();
            }

            if (dt != null && dt.Rows.Count > 0)
            {
                value = Convert.ToInt32(dt.Rows[0][0]);
            }

            return value;
        }

         /// <summary>
        /// Lay thong tin file theo id
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static DataTable Select_AppPlist_by_ID(int ID)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_AppPlist_by_ID(ID);
                db.Close();
            }

            return dt;
        }

        /// <summary>
        /// Lay thong tin file theo AppKey
        /// </summary>
        /// <param name="AppKey"></param>
        /// <returns></returns>
        public static DataTable Select_AppPlist_by_AppKey(string AppKey)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_AppPlist_by_AppKey(AppKey);
                db.Close();
            }

            return dt;
        }

        /// <summary>
        /// danh sach ung dung jailbreak hay ko jailbreak
        /// </summary>
        /// <param name="JB">Jailbreak</param>
        /// <param name="Published">Published</param>
        /// <returns></returns>
        public static DataTable Select_AppPlist_JB(bool JB, bool Published)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_AppPlist_JB(JB, Published);
                db.Close();
            }

            return dt;
        }

        /// <summary>
        /// danh sach ung dung jailbreak hay ko jailbreak
        /// </summary>
        /// <param name="JB">Jailbreak</param>
        /// <param name="Published">Published</param>
        /// <param name="AppMode">Phi cong hoac Tiep vien</param>
        /// <returns></returns>
        public static DataTable Select_AppPlist_JB(bool JB, bool Published, int AppMode)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_AppPlist_JB_w_AppMode(JB, Published, AppMode);
                db.Close();
            }

            return dt;
        }

        public static DataTable Select_AppPlist_Published(bool Published)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_AppPlist_Published(Published);
                db.Close();
            }

            return dt;
        }

        /// <summary>
        /// Danh sach ung dung Testing
        /// </summary>
        /// <param name="Testing"></param>
        /// <returns></returns>
        public static DataTable Select_AppPlist_Testing(bool Testing)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_AppPlist_Testing(Testing);
                db.Close();
            }

            return dt;
        }

        /// <summary>
        /// Them moi user.Tra ve 0: them moi thanh cong; 1: da co user
        /// </summary>
        /// <param name="Username">Ten nguoi dung</param>
        /// <param name="Password">Mat khau</param>
        /// <param name="Udid">Udid</param>
        /// <param name="PartnerCode"></param>
        /// <param name="SupperUser"></param>
        /// <returns>Tra ve 0: them moi thanh cong; 1: da co user</returns>
        public static DataTable Insert_User(string Username, string Password, string Udid, string PartnerCode, bool SupperUser, int AppMode, bool Actived, bool Disable, string Email)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Insert_User(Username, Password, Udid, PartnerCode, SupperUser, AppMode, Actived, Disable, Email);
                db.Close();
            }

            return dt;
        }

        /// <summary>
        /// Lay danh sach user theo partner code: PartnerCode =0: lay tat, con lai lay theo code
        /// </summary>
        /// <param name="PartnerCode"></param>
        /// <returns></returns>
        public static DataTable Select_User(string PartnerCode)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_User(PartnerCode);
                db.Close();
            }

            return dt;
        }

        /// <summary>
        /// Lay thong tin user theo username vaf pasrtnercode
        /// </summary>
        /// <param name="Username"></param>
        /// <param name="PartnerCode"></param>
        /// <returns></returns>
        public static DataTable Select_User_by_Username(string Username, string PartnerCode)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_User_by_Username(Username, PartnerCode);
                db.Close();
            }

            return dt;
        }

        public static void Update_User(string Username, string PartnerCode, string Password, string Udid, bool SupperUser, int AppMode, bool Actived, bool Disable, string Email)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Update_User(Username, PartnerCode, Password, Udid, SupperUser, AppMode, Actived, Disable, Email);
                db.Close();
            }
        }

        public static void Delete_User(string Username, string PartnerCode)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Delete_User(Username, PartnerCode);
                db.Close();
            }
        }

        public static void Insert_FTP(string Udid, string PartnerCode, string HostFTP, string PortFTP, string UserFTP, string PassFTP, bool Passive, string Name, int Type)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Insert_FTP(Udid, PartnerCode, HostFTP, PortFTP, UserFTP, PassFTP, Passive, Name, Type);
                db.Close();
            }
        }

        public static DataTable Select_FTP(string PartnerCode)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_FTP(PartnerCode);
                db.Close();
            }

            return dt;
        }

        public static DataTable Select_FTP(string partnerCode, int type)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_FTP_by_PartnerCode_Type(partnerCode, type);
                db.Close();
            }

            return dt;
        }

        public static DataTable Select_Menu(int ID)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_Menu(ID);
                db.Close();
            }

            return dt;
        }

        public static DataTable Login(string Username, string Password)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Login(Username, Password);
                db.Close();
            }
            return dt;
        }

        public static DataTable Login(string Username, string Password, int AppMode)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Login(Username, Password, AppMode);
                db.Close();
            }
            return dt;
        }

        public static void Insert_FTP(string PartnerCode, string HostFTP, string PortFTP, string UserFTP, 
            string PassFTP, bool Passive, string Name, int Type, bool TSL, bool Active)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Insert_FTP(PartnerCode, HostFTP, PortFTP, UserFTP, PassFTP, Passive, Name, Type, TSL, Active);
                db.Close();
            }
        }

        public static void Update_FTP(int ID, string PartnerCode, string HostFTP, string PortFTP, string UserFTP, 
            string PassFTP, bool Passive, string Name, int Type, bool TSL, bool Active)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Update_FTP(ID, PartnerCode, HostFTP, PortFTP, UserFTP, PassFTP, Passive, Name, Type, TSL, Active);
                db.Close();
            }
        }

        public static DataTable Select_FTP(int ID)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_FTP_by_ID(ID);
                db.Close();
            }

            return dt;
        }

        public static void Delete_FTP(int ID)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Delete_FTP(ID);
                db.Close();
            }
        }

        public static DataTable Select_Menu_by_User(string Username)
        {
            DataTable dt = new DataTable();

            string cacheName = Cached.CacheName.Cache_Select_Menu_by_User(Username);
            dt = Cached.GetCache<DataTable>(cacheName);
            if (dt == null)
            {
                using (MainDB db = new MainDB())
                {
                    dt = db.StoredProcedures.VNA_Select_Menu_by_User(Username);
                    db.Close();
                }

                Cached.SetCache(cacheName, dt);
            }

            return dt;
        }
        /// <summary>
        /// Lay thong tin menu theo ID
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static DataTable Select_Menu_by_ID(int ID)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_Menu_by_ID(ID);
                db.Close();
            }

            return dt;
        }

        /// <summary>
        /// Lay thong tin menu theo danh sach id cach nhau = dau ,
        /// </summary>
        /// <param name="IDs"></param>
        /// <returns></returns>
        public static DataTable Select_Menu_by_ID(string IDs)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_Menu_by_ID(IDs);
                db.Close();
            }

            return dt;
        }

        /// <summary>
        /// Lay ID cac menu ma user duoc quyen truy cap
        /// </summary>
        /// <param name="Username"></param>
        /// <returns></returns>
        public static DataTable Select_User_Menu(string Username)
        {
            string cacheName = Cached.CacheName.Cache_Select_User_Menu(Username);
            DataTable dt = new DataTable();
            dt = Cached.GetCache<DataTable>(cacheName);
            if (dt == null)
            {
                using (MainDB db = new MainDB())
                {
                    dt = db.StoredProcedures.VNA_Select_User_Menu(Username);
                    db.Close();
                }

                Cached.SetCache(cacheName, dt);
            }

            return dt;
        }

        public static DataTable Select_Menu_by_Url(string Url)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_Menu_by_Url(Url);
                db.Close();
            }

            return dt;
        }

        public static DataTable Select_MenuChild_by_User(string Username)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_MenuChild_by_User(Username);
                db.Close();
            }

            return dt;
        }
        public static DataTable Select_MenuChild_All()
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_MenuChild_All();
                db.Close();
            }

            return dt;
        }

        public static DataTable Select_Menu_Parent()
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_Menu_Parent();
                db.Close();
            }            
            return dt;
        }

        public static void Update_User_Menu(string Username, string Menu_ID)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Update_User_Menu(Username, Menu_ID);
                db.Close();
            }
        }

        public static void Insert_User_Menu(string Username, string Menu_ID)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Insert_User_Menu(Username, Menu_ID);
                db.Close();
            }
        }

        public static void Insert_Menu(string Name, int ParentID, bool Visible, string Url, int Order, bool AlwaysVisible)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Insert_Menu(Name, ParentID, Visible, Url, Order, AlwaysVisible);
                db.Close();
            }
        }

        public static void Update_Menu(int ID, string Name, int ParentID, bool Visible, string Url, int Order, bool AlwaysVisible)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Update_Menu(ID, Name, ParentID, Visible, Url, Order, AlwaysVisible);
                db.Close();
            }
        }

        public static DataTable Select_Menu_All()
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_Menu_All();
                db.Close();
            }

            return dt;
        }
        public static DataTable Select_AppMode()
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_AppMode();
                db.Close();
            }

            return dt;
        }

        public static DataTable Select_AppPlist_By_AppMode(int AppMode)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_AppPlist_By_AppMode(AppMode);
                db.Close();
            }

            return dt;
        }

        public static DataTable Select_DownloadCounter(string PartnerCode)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_DownloadCounter(PartnerCode);
                db.Close();
            }

            return dt;
        }

        public static void Insert_DownloadCounter(string PartnerCode)
        {
            
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Insert_DownloadCounter(PartnerCode);
                db.Close();
            }

            
        }

        public static void Update_DownloadCounter(string PartnerCode, int Counter, int CounterLimit)
        {

            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Update_DownloadCounter(PartnerCode, Counter, CounterLimit);
                db.Close();
            }
        }

        public static DataTable Select_Devices_All_by_Email(string email)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_Devices_All_by_Email(email);
                db.Dispose();
            }

            return dt;
        }

        public static DataTable Select_Devices_Blacklist_by_Email(string email)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_Devices_Blacklist_by_Email(email);
                db.Dispose();
            }

            return dt;
        }

        public static void Update_Device_Active(int id, bool active)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Update_Device_Active(id, active);
                db.Dispose();
            }
        }

        public static DataTable Select_DevicesDownload_by_Month(DateTime dateTime)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_DevicesDownload_by_Month(dateTime);
                db.Dispose();
            }

            return dt;
        }

        public static DataTable Select_LogSync_by_Uuid(string Uuid)
        {
            DataTable dt = new DataTable();
            using(MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_LogSync_by_Uuid(Uuid);
                db.Dispose();
            }

            return dt;
        }

        /// <summary>
        /// Lay danh sach cac device da active theo type : 1: theo gioi; 2: ngay; 3: thang; 4: nam
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static DataTable Report_Device_Active(int type)
        {
            DataTable dt = new DataTable();
            using(MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Report_Device_Active(type);
                db.Dispose();
            }

            return dt;
        }

        public static DataTable Select_AppName_by_PartnerCode(string partnerCode)
        {
            DataTable dt = new DataTable();

            using(MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_AppName_by_PartnerCode(partnerCode);
                db.Close();
                db.Dispose();
            }

            return dt;
        }

        /// <summary>
        /// Neu gia tri tra ve la 1 : da ton tai ban nghi; 0 : insert thanh cong
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="uuid"></param>
        /// <param name="note"></param>
        /// <returns></returns>
        public static int Insert_Blacklist(string appId, string uuid, string note)
        {
            int result = 1;
            using (MainDB db = new MainDB())
            {
                DataTable dt = db.StoredProcedures.VNA_Insert_Blacklist(appId, uuid, note);
                db.Close();
                db.Dispose();

                if(dt!=null)
                {
                    result = Convert.ToInt32(dt.Rows[0][0]);
                }
            }

            return result;
        }

        public static void Delete_Blacklist(int id)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Delete_Blacklist(id);
                db.Close();
                db.Dispose();
            }
        }

        public static DataTable Select_Blacklist(int id)
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_Blacklist_by_ID(id);
                db.Close();
                db.Dispose();
            }

            return dt;
        }

        public static DataTable Select_Blacklist()
        {
            DataTable dt = new DataTable();

            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_Blacklist();
                db.Close();
                db.Dispose();
            }
            return dt;
        }

        /// <summary>
        /// Danh sach cac menu luon hien thi
        /// </summary>
        /// <param name="partnerCode"></param>
        /// <returns>DataTable</returns>
        public static DataTable Select_Menu_AlwaysVisible()
        {
            DataTable dt = new DataTable();
            using(MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_Menu_AlwaysVisible();
                db.Close();
                db.Dispose();
            }
            return dt;
        }

        /// <summary>
        /// Them moi Email Supper
        /// </summary>
        /// <param name="email"></param>
        /// <param name="isCompany"></param>
        public static void Insert_Emails(string email, bool isCompany)
        {
            using(MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Insert_Emails(email, isCompany);
                db.Close();
                db.Dispose();
            }
        }

        /// <summary>
        /// Xoa Email Supper
        /// </summary>
        /// <param name="id"></param>
        public static void Delete_Emails(int id)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.VNA_Delete_Emails(id);
                db.Close();
                db.Dispose();
            }
        }

        /// <summary>
        /// Lay thong tin Emails theo email
        /// </summary>
        /// <param name="Email"></param>
        /// <returns></returns>
        public static DataTable Select_Emails_by_Email(string Email)
        {
            DataTable dt = new DataTable();
            using(MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_Emails_by_Email(Email);
                db.Close();
                db.Dispose();
            }

            return dt;
        }

        /// <summary>
        /// Lay tat ca cac Email Supper
        /// </summary>
        /// <returns></returns>
        public static DataTable Select_Emails()
        {
            DataTable dt = new DataTable();
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.VNA_Select_Emails();
                db.Close();
                db.Dispose();
            }

            return dt;
        }
    }
}
